﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EpiNEXT.WPF
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:EpiNEXT.WPF"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:EpiNEXT.WPF;assembly=EpiNEXT.WPF"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:ImagePicker/>
    ///
    /// </summary>
    public class ImagePicker : Control
    {
        static ImagePicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImagePicker), new FrameworkPropertyMetadata(typeof(ImagePicker)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var editButton = this.GetTemplateChild("buttonEdit") as CircleButton;
            if (editButton != null)
            {
                editButton.Click += editButton_Click;
            }

            var deleteButton = this.GetTemplateChild("buttonDelete") as CircleButton;
            if (deleteButton != null)
            {
                deleteButton.Click += deleteButton_Click;
            }
        }

        void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            this.ImageSource = null;
        }

        void editButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.AutoUpgradeEnabled = true;
            ofd.CheckFileExists = true;

            ofd.DefaultExt = "png";
            ofd.Filter = "Portable Network Graphics files (*.png)|*.png";

            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            System.Windows.Forms.DialogResult result = ofd.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                BitmapImage userImage = new BitmapImage();
                userImage.BeginInit();
                    
                userImage.UriSource = new Uri("file:///" + ofd.FileName);
                byte[] imageBytes = System.IO.File.ReadAllBytes(ofd.FileName);
                
                userImage.EndInit();

                userImage.Freeze();

                ImageSource = userImage;
            }
        }

        public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImagePicker), new PropertyMetadata(null));
        public ImageSource ImageSource
        {
            get
            {
                return (ImageSource)(this.GetValue(SourceProperty));
            }
            set
            {
                this.SetValue(SourceProperty, value);
            }
        }

        public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(ImagePicker), new PropertyMetadata(Double.NaN));
        public double ImageHeight
        {
            get
            {
                return (double)(this.GetValue(ImageHeightProperty));
            }
            set
            {
                this.SetValue(ImageHeightProperty, value);
            }
        }

        public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(ImagePicker), new PropertyMetadata(Double.NaN));
        public double ImageWidth
        {
            get
            {
                return (double)(this.GetValue(ImageWidthProperty));
            }
            set
            {
                this.SetValue(ImageWidthProperty, value);
            }
        }
    }
}
